package common.com.androidbase.datarequest;

import android.net.Uri;

import com.android.sohu.sdk.common.toolbox.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 静态域名替换
 * Created by jayyang on 2015/11/16.
 */
public class StaticDomains {
    private final static String FORMAL_STATIC_DOMAIN_API_TV = "http://s1.api.tv.itc.cn";
    private static List<String> staticReplaceList = new ArrayList<>();

    static {
        // 升级系统
        staticReplaceList.add("/mobile_user/sdk/get.json");
        staticReplaceList.add("/mobile_user/soversion/checkver.json");
        staticReplaceList.add("/mobile_user/version/checkver.json");
        // MVMS
        staticReplaceList.add("/v4/mobile/feeling/list.json");
        staticReplaceList.add("/v4/mobile/share/aids.json");
        staticReplaceList.add("/v4/mobile/clarity/config.json");
        staticReplaceList.add("/v4/mobile/subject/api/list");
        staticReplaceList.add("/v4/mobile/intervene/hotkey.json");
        staticReplaceList.add("/v4/mobile/subject/html/list");
        staticReplaceList.add("/v5/mobile/classificationScreening/list.json");
        staticReplaceList.add("/v6/mobile/h5/helpList.json");
        staticReplaceList.add("/v4/mobile/coldStart/list.json");
        staticReplaceList.add("/v6/mobile/h5/helpContentList.json");
        staticReplaceList.add("/v4/mobile/hotApp/list.json");
        staticReplaceList.add("/v5/mobile/channel/list.json");
        staticReplaceList.add("/v4/mobile/searcher/hot.json");
        staticReplaceList.add("/v4/mobile/star/top.json");
        staticReplaceList.add("/v5/mobile/channelPageData/list.json");
        staticReplaceList.add("/mobile/channel/list.json");
        staticReplaceList.add("/v4/mobile/widget/list.json");
        staticReplaceList.add("/mobile/loading/list.json");
        staticReplaceList.add("/v4/mobile/stream/ad");
        staticReplaceList.add("/v4/mobile/channel/list.json");
        staticReplaceList.add("/v5/mobile/homePage/list.json");
        staticReplaceList.add("/v6/mobile/classificationScreening/list.json");
        staticReplaceList.add("/mobile/column/list.json");
        staticReplaceList.add("/v4/mobile/searcher/hotList.json");
        staticReplaceList.add("/v4/mobile/channelPageData/list.json");
        staticReplaceList.add("/v4/mobile/activity.json");
        staticReplaceList.add("/v4/mobile/plugin/list.json");
        staticReplaceList.add("/v4/mobile/column/list.json");
        staticReplaceList.add("/v4/mobile/loading/list.json");
        staticReplaceList.add("/v4/mobile/adv.json");
        staticReplaceList.add("/mobile/video/list.json");
        staticReplaceList.add("/v6/mobile/channel/list.json");
        staticReplaceList.add("/v4/mobile/control/switch.json");
        staticReplaceList.add("/mobile/control/switch.json");
        staticReplaceList.add("/v6/mobile/channelPageData/list.json");
        staticReplaceList.add("/v6/mobile/h5/list.json");
        staticReplaceList.add("/v4/mobile/video/list.json");
        // GAOYANG
        staticReplaceList.add("/v4/tag/video/tags/");
        staticReplaceList.add("/v4/video/pgc/rank/");
        staticReplaceList.add("/v4/user/info/");
        staticReplaceList.add("/v4/user/list.json");
        staticReplaceList.add("/v4/user/recommend/pgc.json");
        staticReplaceList.add("/v4/user/album.json");
        staticReplaceList.add("/v4/user/video.json");
        staticReplaceList.add("/v4/tag/album/tags/");
        staticReplaceList.add("/v4/search/hots.json");
        staticReplaceList.add("/v4/program/");
        staticReplaceList.add("/v4/search/black_tv_list");
        staticReplaceList.add("/v4/search/channel/hot");
        staticReplaceList.add("/v4/album/videos/");
        staticReplaceList.add("/v4/album/trailers/");
        staticReplaceList.add("/v4/album/zongyi/");
        staticReplaceList.add("/v4/star/fans_ranks.json");
        staticReplaceList.add("/v4/star/praise_info.json");
        staticReplaceList.add("/v4/star/works/video.json");
        staticReplaceList.add("/v4/star/info/");
        staticReplaceList.add("/v4/star/ranks.json");
        staticReplaceList.add("/v4/star/works/album.json");
        staticReplaceList.add("/v4/star/work/stars.json");
        staticReplaceList.add("/v4/search/top.json");
        staticReplaceList.add("/v4/album/batch/latest.json");
        staticReplaceList.add("/v4/search/channel.json");
        staticReplaceList.add("/v4/category/");
        staticReplaceList.add("/v4/search/stream/");
        staticReplaceList.add("/v4/search/stream/recommend.json");
        staticReplaceList.add("/v4/album/video_stream/");
        staticReplaceList.add("/v4/video/share.json");
        staticReplaceList.add("/v4/video/download_url/");
        staticReplaceList.add("/v4/video/info/");
        // TODO 测试代码
        staticReplaceList.add("/v4/video/inf/");
        staticReplaceList.add("/v4/album/video_stream/");
        staticReplaceList.add("/v4/search/recommend/hot.json");
        staticReplaceList.add("/v4/search/recommend.json");
        staticReplaceList.add("/v4/search/recommend/cf.json");
        staticReplaceList.add("/v4/album/info/");
        staticReplaceList.add("/v4/search/channel/sub.json");
    }

    /**
     * 动态域名替换为静态域名
     * note：只替换http://api.tv.sohu.com开头的域名，不替换测试域名
     * @param url
     * @return
     */
    public static String replaceStaticDomain(String url) {
        if (StringUtils.isEmpty(url) || !url.startsWith("http://api.tv.sohu.com")) {
            return url;
        }
        for (String str : staticReplaceList) {
            if (url.contains(str)) {
                System.out.println(url);
                System.out.println(Uri.parse(url));
                String host = Uri.parse(url).getHost();
                if (StringUtils.isEmpty(host)) {
                    break;
                }
                if (host.endsWith("/")) {
                    host = host.substring(0, host.length() - 1);
                }
                if (!host.startsWith("http://")) {
                    host = "http://" + host;
                }
                return url.replace(host, FORMAL_STATIC_DOMAIN_API_TV);
            }
        }
        return url;
    }

}
